package com.bawei.module_advice.adapter


import android.annotation.SuppressLint
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_advice.R
import com.bawei.module_advice.databinding.ItemAdviceBinding
import com.bawei.module_advice.model.AdviceModel

import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class AdviceAdapter:RecyclerView.Adapter<AdviceAdapter.AdviceViewMolder>() {

    private val adviceList= mutableListOf<AdviceModel>()
    fun setData(data:List<AdviceModel>){
        adviceList.clear()
        adviceList.addAll(data)
        notifyDataSetChanged()
        //1
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdviceViewMolder {
        val binding=ItemAdviceBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return AdviceViewMolder(binding)
    }

    override fun getItemCount(): Int {
        return adviceList.size
        Log.d("Fcc","count"+adviceList.size)

    }

    @SuppressLint("ResourceAsColor")
    override fun onBindViewHolder(holder: AdviceViewMolder, position: Int) {
        val binding=DataBindingUtil.getBinding<ItemAdviceBinding>(holder.itemView)?:return
        binding.adviceName.text=adviceList[position].title

        if (adviceList[position].id%2==0){
            Glide.with(holder.itemView)
                .load(R.mipmap.a1)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(24))) // 圆角半径
                .into(binding.adviceImg);
        }else if (adviceList[position].id%2==1){
            Glide.with(holder.itemView)
                .load(R.mipmap.a2)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(24))) // 圆角半径
                .into(binding.adviceImg);
        }else{
            Glide.with(holder.itemView)
                .load(R.mipmap.a3)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(24))) // 圆角半径
                .into(binding.adviceImg);
        }

        binding.adviceText.text=adviceList[position].content
        binding.adviceTime.text=adviceList[position].publishTime
        if (!adviceList[position].status.isNullOrEmpty()){

            binding.baoMing.setText("已读")
            binding.baoMing.setTextColor(R.color.b3)
            binding.baoMing.setBackgroundResource(R.drawable.rounded_corners3)
        }else{

            binding.baoMing.setText("立即报名")
            binding.baoMing.setTextColor(Color.WHITE)
            binding.baoMing.setOnClickListener {
                adviceList[position].status="1"
                notifyDataSetChanged()
            }
            binding.baoMing.setBackgroundResource(R.drawable.rounded_corners2)
        }




    }


    class AdviceViewMolder (item: ItemAdviceBinding):RecyclerView.ViewHolder(item.root)

}